Skip to content

Add a pluggable subscription bus for subscriptions/listen streams - #744

Open
koic wants to merge 1 commit into
modelcontextprotocol:mainfrom
koic:add_pluggable_subscription_bus
Open

koic wants to merge 1 commit into
modelcontextprotocol:mainfrom
koic:add_pluggable_subscription_bus

Conversation

@koic

@koic koic commented Dec 3, 2026

Copy link
Copy Markdown
Member

Motivation and Context

The subscriptions/listen registry is per-process: a stream lives in the worker that accepted it, so a change published in another worker (Puma clustered mode, or several instances behind a load balancer) never reached it. This was the one remaining piece of cross-process state on the modern lifecycle; sessions, SSE resumability, and server-initiated requests are gone as of MCP 2026-07-28, and the MRTR pattern is specified to work "without requiring a shared storage layer across server instances". Both reference SDKs close the gap with the same seam - the Python SDK's SubscriptionBus protocol and the TypeScript SDK's ServerEventBus interface, each with an in-memory default and user-supplied brokers - and this ports that seam.

MCP::Server::SubscriptionBus carries the four typed change events of MCP::Subscriptions (ToolsListChanged, PromptsListChanged, ResourcesListChanged, and ResourceUpdated with its URI), never JSON-RPC: acknowledgements, per-stream filter matching, and subscriptionId stamping stay in the transport, so a bus implementation can only move events between processes. StreamableHTTPTransport publishes every change through the bus and feeds its local streams from a bus listener registered at construction, making the arrival path identical for local and remote events; the in-process default publishes back synchronously, preserving the single-process delivery behavior. A multi-process deployment passes subscription_bus: backed by its own pub/sub system; the docs show a Redis sketch, and no broker adapter is bundled, matching both reference SDKs.

MCP::Subscriptions is registered in the autoload list of mcp.rb like every other public constant, so a custom bus that rebuilds events from its broker at boot resolves the classes before any transport exists. A publish that raises, a broker outage, say, is reported through the exception reporter the way a raising listener or unsubscribe already is, so the legacy streams and modern sinks of the publishing worker still receive the change; the change senders rescue on their own, so what such a failure used to cost was that local delivery. The typed round trip carries only what the specification's params hold, the event kind and the resource URI, so an extension member a caller adds to one of the four change notifications reaches the legacy streams and modern sinks but not the listen streams, as in the Python SDK. The docs state that the default bus belongs to one transport, and the Redis sketch rescues per listener, like the default bus, with a separate rescue for a message the worker cannot decode. The page also states the contract a custom bus honors (an idempotent unregistering callable, listeners run outside the bus's locks on the receiving thread, and one already selected may still run after unregistering, which each stream's closed check absorbs), that with the default bus delivery is synchronous on the calling thread, so a stalled stream delays the notifier and a broker round trip in publish belongs behind a timeout, that the broker needs the same trust as the server processes, and what a production adapter adds to the sketch: waiting for the subscription before serving traffic, supervising the reader, stopping it on close, and building the bus after the fork. The class comment carries the same contract, and the close comment attributes the teardown ordering to each stream's write mutex and closed flag rather than to the unsubscribe.
The transport does not depend on a custom bus honoring that contract. Its listener does nothing once the transport is closed or could not be built, so a listener left on the bus by an unregistering callable that raises, or by a subscribe that raises after registering, writes to no stream. An exception reporter that raises while one of the bus failures is reported is ignored, since the exception would end the reader thread of a broker, stop close, or cost the other streams of the worker the change.

How Has This Been Tested?

With the new bus unit tests and transport tests (including two transports sharing one bus standing in for two workers), the full suite, RuboCop, and the conformance suite, all green; the docs preview builds with every internal link and anchor check passing. A subprocess test resolves MCP::Subscriptions right after require "mcp", and a bus whose publish raises is reported under its own context while a legacy GET stream still receives the change; both fail against the previous tree. Six more tests keep a listener on a bus whose unregistering callable or subscribe raises and report through a reporter that raises; each fails without the guard it covers.

Breaking Changes

None. The bus is a new optional keyword argument, and the in-process default preserves the existing single-process delivery behavior, apart from the extension members noted above, which no longer reach the listen streams.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Test update (non-breaking change which adds or updates tests)
  • Refactoring (non-breaking change which makes things better)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • CI/build script update (update CI workflow or build scripts)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • I have added automated tests for my change
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

## Motivation and Context

The `subscriptions/listen` registry is per-process: a stream lives in the worker that accepted it,
so a change published in another worker (Puma clustered mode, or several instances behind a load balancer)
never reached it. This was the one remaining piece of cross-process state on the modern lifecycle;
sessions, SSE resumability, and server-initiated requests are gone as of MCP 2026-07-28,
and the MRTR pattern is specified to work "without requiring a shared storage layer across server instances".
Both reference SDKs close the gap with the same seam - the Python SDK's `SubscriptionBus` protocol and
the TypeScript SDK's `ServerEventBus` interface, each with an in-memory default and user-supplied brokers -
and this ports that seam.

`MCP::Server::SubscriptionBus` carries the four typed change events of `MCP::Subscriptions` (`ToolsListChanged`,
`PromptsListChanged`, `ResourcesListChanged`, and `ResourceUpdated` with its URI), never JSON-RPC: acknowledgements,
per-stream filter matching, and subscriptionId stamping stay in the transport, so a bus implementation can only
move events between processes. `StreamableHTTPTransport` publishes every change through the bus and feeds its
local streams from a bus listener registered at construction, making the arrival path identical for local and
remote events; the in-process default publishes back synchronously, preserving the single-process delivery behavior.
A multi-process deployment passes `subscription_bus:` backed by its own pub/sub system; the docs show a Redis sketch,
and no broker adapter is bundled, matching both reference SDKs.

`MCP::Subscriptions` is registered in the autoload list of `mcp.rb` like every other public constant, so a custom bus
that rebuilds events from its broker at boot resolves the classes before any transport exists. A `publish` that raises,
a broker outage, say, is reported through the exception reporter the way a raising listener or unsubscribe already is,
so the legacy streams and modern sinks of the publishing worker still receive the change; the change senders rescue
on their own, so what such a failure used to cost was that local delivery. The typed round trip carries only what
the specification's params hold, the event kind and the resource URI, so an extension member a caller adds to one of
the four change notifications reaches the legacy streams and modern sinks but not the listen streams, as in the Python SDK.
The docs state that the default bus belongs to one transport, and the Redis sketch rescues per listener, like the default bus,
with a separate rescue for a message the worker cannot decode.
The page also states the contract a custom bus honors (an idempotent unregistering callable, listeners run outside
the bus's locks on the receiving thread, and one already selected may still run after unregistering,
which each stream's closed check absorbs), that with the default bus delivery is synchronous on the calling thread, so a stalled stream delays
the notifier and a broker round trip in `publish` belongs behind a timeout, that the broker needs the same trust as the server processes,
and what a production adapter adds to the sketch: waiting for the subscription before serving traffic, supervising
the reader, stopping it on close, and building the bus after the fork. The class comment carries the same contract,
and the `close` comment attributes the teardown ordering to each stream's write mutex and closed flag rather than
to the unsubscribe.
The transport does not depend on a custom bus honoring that contract. Its listener does nothing once the transport is closed
or could not be built, so a listener left on the bus by an unregistering callable that raises, or by a `subscribe` that raises
after registering, writes to no stream. An exception reporter that raises while one of the bus failures is reported is ignored,
since the exception would end the reader thread of a broker, stop `close`, or cost the other streams of the worker the change.

## How Has This Been Tested?

With the new bus unit tests and transport tests (including two transports sharing one bus standing in for two workers),
the full suite, RuboCop, and the conformance suite, all green; the docs preview builds with every internal link and anchor check passing.
A subprocess test resolves `MCP::Subscriptions` right after `require "mcp"`, and a bus whose `publish` raises is reported under
its own context while a legacy GET stream still receives the change; both fail against the previous tree.
Six more tests keep a listener on a bus whose unregistering callable or `subscribe` raises and report through a reporter that raises;
each fails without the guard it covers.

## Breaking Changes

None. The bus is a new optional keyword argument, and the in-process default preserves the existing single-process delivery behavior,
apart from the extension members noted above, which no longer reach the listen streams.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant